package io.renren.modules.sys.service.impl;

import io.renren.modules.sys.dao.QAnswerDao;
import io.renren.modules.sys.dto.QAnswerDTO;
import io.renren.modules.sys.entity.QAnswer;
import io.renren.modules.sys.entity.QQuestionnaires;
import io.renren.modules.sys.service.QAnswerService;
import jakarta.annotation.Resource;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * (QAnswer)表服务实现类
 *
 * @author makejava
 * @since 2024-07-03 16:24:18
 */
@Service("qAnswerService")
public class QAnswerServiceImpl implements QAnswerService {
    @Resource
    private QAnswerDao qAnswerDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public QAnswer queryById(String id) {
        return this.qAnswerDao.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param qAnswer 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @Override
    public Page<QAnswer> queryByPage(QAnswer qAnswer, PageRequest pageRequest) {
        long total = this.qAnswerDao.count(qAnswer);
        return new PageImpl<>(this.qAnswerDao.queryAllByLimit(qAnswer, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param qAnswer 实例对象
     * @return 实例对象
     */
    @Override
    public QAnswer insert(QAnswer qAnswer) {
        this.qAnswerDao.insert(qAnswer);
        return qAnswer;
    }

    @Override
    public QAnswer insertOrUpdate(QAnswer qAnswer) {
        this.qAnswerDao.insertOrUpdate(qAnswer);
        return qAnswer;
    }

    /**
     * 修改数据
     *
     * @param qAnswer 实例对象
     * @return 实例对象
     */
    @Override
    public QAnswer update(QAnswer qAnswer) {
        this.qAnswerDao.update(qAnswer);
        return this.queryById(qAnswer.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(String id) {
        return this.qAnswerDao.deleteById(id) > 0;
    }

    @Override
    public List<QAnswerDTO> queryAnswersByAnswererIdAndQuestionId(String answererId, List<String> questionId) {
        String answererIdStr = String.join(",", questionId);
        return this.qAnswerDao.queryAnswersByAnswererIdAndQuestionId(answererId, questionId, answererIdStr);
    }
}
